import {
    FETCH_COURSES_ERROR,
    FETCH_COURSES_REQUEST,
    FETCH_COURSES_SUCCESS, CHANGE_CREATOR_FORM,
    INIT_CREATOR_FORM,
    POST_COURSE_SUCCESS, CHANGE_CREATOR_FORM_MULTIPART, CLOSE_CREATE_SUCCESS_MODAL
} from '../constants'
import {SET_CURRENT_DETAIL} from '../constants'
import {formatDate} from "../utils/format";

const INITIAL_STATE = {
    ids: [],
    byId: {},
    currentDetail: null,
    creatorForm: {
        // title: '',
        // description: '',
        // event_agenda: '', // 议程
        // startTime: '',
        // startDate: '',
        // endTime: '',
        // endDate: '',
        // place: '',
        // address: '',
        // latitude: '',
        // longitude: '',
        // banner_blob_id: '',
        //
        pickedImages: []
    },
    showCreateSuccessModal: false
}


export default function courseList(state = INITIAL_STATE, action) {
    switch (action.type) {
        case FETCH_COURSES_REQUEST:
            return state
        case FETCH_COURSES_SUCCESS:
            const convertedData = convertCourseList(action.data);
            return {...state, ...convertedData}
        case FETCH_COURSES_ERROR:
            return state
        case SET_CURRENT_DETAIL:
            return {
                ...state,
                currentDetail:
                    state.byId !== {} ? state.byId[action.id] : null
            }
        case POST_COURSE_SUCCESS:
            const addedState = addCourse(state, action.data)
            return {...addedState, creatorForm: {},showCreateSuccessModal: true}
        case INIT_CREATOR_FORM:
            const now = Date.now()
            const after2Hour = now + 2 * 3600 * 1000
            const startDate = formatDate(now, 'yyyy-MM-dd')
            const startTime = formatDate(now, 'hh:mm')
            const endDate = formatDate(after2Hour, 'yyyy-MM-dd')
            const endTime = formatDate(after2Hour, 'hh:mm')
            return {
                ...state,
                creatorForm: {
                    ...state.creatorForm,
                    startDate: startDate,
                    startTime: startTime,
                    endDate: endDate,
                    endTime: endTime
                }
            }
        case CHANGE_CREATOR_FORM:
            return {
                ...state,
                creatorForm: {...state.creatorForm, [action.field]: action.value}
            }
        case CHANGE_CREATOR_FORM_MULTIPART:
            return {
                ...state,
                creatorForm: {...state.creatorForm, ...action.obj}
            }
        case CLOSE_CREATE_SUCCESS_MODAL:
            return {...state, showCreateSuccessModal: false}
        default:
            return state
    }
}

const convertCourseList = function (rawData) {
    const convertedData = {
        ids: [],
        byId: {}
    }
    rawData.map(item => {
        convertedData.ids.push(item.id)
        convertedData.byId[item.id] = item
    })
    return convertedData
}

const addCourse = function (courseList, resData) {
    console.log(resData)
    const newCourse = resConvertToCourse(resData)
    const newCourseList = {...courseList}
    newCourseList.ids.push(newCourse.id)
    newCourseList.byId[newCourse.id] = newCourse
    return newCourseList
}

const resConvertToCourse = function (resData) {
    return {
        ...resData

    }
}
